package com.zyl.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.zyl.dto.UserCommentDTO;
import com.zyl.exception.PictureException;
import com.zyl.pojo.Page;
import com.zyl.pojo.PrivateWord;
import com.zyl.pojo.User;
import com.zyl.service.PrivateWordService;
import com.zyl.service.UserService;
import com.zyl.util.MD5Util;
import com.zyl.util.ResultVoUtil;
import com.zyl.vo.PrivateWordVo;
import com.zyl.vo.ResultVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.sql.Wrapper;
import java.util.List;

/**
 * @Author: Zyl
 * @Param:
 * @return:
 * @Description:
 */
@RestController
@RequestMapping("/user")
public class UserController {
    @Autowired
    private UserService userService;
    @Autowired
    private PrivateWordService privateWordService;

    @GetMapping("/data/{id}")
    public ResultVo findUserInfo(@PathVariable("id") Integer id){
        User user = userService.queryUserById(id);
        return ResultVoUtil.success(user);
    }
    @PostMapping("/data")
    public ResultVo updateUserInfo(@RequestBody User user){
        userService.updateUser(user);
        return ResultVoUtil.success(null);
    }
       @PostMapping("/updatePassword")
        public ResultVo updatePassword(@RequestBody User user){
           System.out.println(user);
            userService.updatePasswordById(user);
            return ResultVoUtil.success(null);
        }
        @PostMapping("/privateWord")
    public ResultVo addPrivateWord(@RequestBody PrivateWord privateWord){

            privateWordService.addPrivateWord(privateWord);
            return ResultVoUtil.success(null);
        }
        @GetMapping("/privateWordResp/{id}")
    public ResultVo respPrivateWord(@PathVariable("id") Integer id){
            List<PrivateWordVo> privateWord = privateWordService.findPrivateWordByPublishId(id);
            return  ResultVoUtil.success(privateWord);
        }

    @RequestMapping("/imgUpload/{id}")
        public ResultVo imgUpload(MultipartHttpServletRequest multiReq,@PathVariable("id") Integer id)
            throws IOException {
        String fileUploadPath="src\\main\\resources\\static\\";
       //我这里用的springboot 在application.properties中配置，使用@Value 获取的文件上传目录
        User user=new User();
        user.setId(id);
        MultipartFile file = multiReq.getFile("file");
        String originalFilename = file.getOriginalFilename();
        String suffix = originalFilename.substring(originalFilename.indexOf("."));
        String localFileName = MD5Util.md5(file.getInputStream()) + suffix;
        File localFile = new File(fileUploadPath + localFileName);
        if (!localFile.exists()) {
            localFile.createNewFile();
            FileOutputStream fos = new FileOutputStream(
                    localFile);
            try {
                FileInputStream fs = (FileInputStream) multiReq.getFile("file").getInputStream();
                byte[] buffer = new byte[1024];
                int len = 0;
                while ((len = fs.read(buffer)) != -1) {
                    fos.write(buffer, 0, len);
                }
                fos.close();
                fs.close();
            } catch (NullPointerException e) {
                e.printStackTrace();
            } finally {
                user.setAvatarImgUrl("http://localhost:8084/"+localFileName);
                userService.updateUserPictureById(user);
                return ResultVoUtil.success("http://localhost:8084/"+localFileName);
            }
        } else {
            System.out.println("已经有文件了可以直接返回");
        }
        user.setAvatarImgUrl("http://localhost:8084/"+localFileName);
        userService.updateUserPictureById(user);
        return  ResultVoUtil.success("http://localhost:8084/"+localFileName);

    }
    @PostMapping("/commentRecord/{respondentId}")
    public ResultVo UserComment(@PathVariable("respondentId") Integer respondentId, @RequestBody Page page){
        PageHelper.offsetPage((page.getCurrentPage() - 1) * page.getPageSize(),page.getPageSize());
        List<UserCommentDTO> list = userService.UserCommentByResponse(respondentId);
        PageInfo<UserCommentDTO> userCommentDTO=new PageInfo<>(list);
        userCommentDTO.setTotal(userService.UserCommentCount(respondentId));
        return ResultVoUtil.success(userCommentDTO);
    }
    @GetMapping("/isReadCount/{respondentId}")
    public ResultVo IsReadCount(@PathVariable("respondentId") Integer respondentId){
        Integer readCount = userService.isReadCount(respondentId);
        return ResultVoUtil.success(readCount);
    }
    @GetMapping("/updateIsRead/{id}")
    public ResultVo updateIsRead(@PathVariable("id") Integer id){
        userService.updateIsRead(id);
        return ResultVoUtil.success(null);
    }
}
